<template>
  <div id="home">
    <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>
    <tab-control @tabClick="itemClick"
                 :titles="['流行', '新款', '精选']" ref="tabControl1"
                 class="tabControl" v-show="isTabFixed"/>
    <scroll class="scroll" :probe-type="3" :pull-up-load="true" ref="scroll"
            @scrollEvent="scrollBack" @pullUp="upLoadMore">
      <home-swiper :banners="banners" @imgLoad="swiperLoad"/>
      <recommend-view :recommends="recommends"/>
      <feature-view/>
      <tab-control @tabClick="itemClick"
                   :titles="['流行', '新款', '精选']" ref="tabControl2"/>
      <goods-list :goods="showGoods" />
    </scroll>
    <back-top @click.native="backClick" v-show="isShowBack"/>
  </div>
</template>

<script>
  import HomeSwiper from "./childComps/HomeSwiper"
  import RecommendView from "./childComps/RecommendView"
  import FeatureView from "./childComps/FeatureView"

  import NavBar from "components/common/navbar/NavBar"
  import Scroll from "components/common/scroll/Scroll"
  import TabControl from "components/content/tabControl/TabControl"
  import GoodsList from 'components/content/goods/GoodsList'

  import {getHomeMutidata, getHomeGoods} from "network/home"
  import {debounce} from "common/utils"
  import {backTopMixin} from 'common/mixin'

  export default {
    name: "Home",
    data(){
      return{
        banners: [],
        recommends: [],
        goods: {
          pop: { page: 0, list: [] },
          new: { page: 0, list: [] },
          sell: { page: 0, list: [] }
        },
        currentType: 'pop',
        offsetTopParent: 0,
        isTabFixed: false,
        scrollY: 0
      }
    },
    mixins: [backTopMixin],
    components: {
      NavBar,
      HomeSwiper,
      RecommendView,
      FeatureView,
      TabControl,
      GoodsList,
      Scroll
    },
    created() {
      this.getHomeMutidata()
      this.getHomeGoods('pop')
      this.getHomeGoods('new')
      this.getHomeGoods('sell')

      /*this.$bus.$on('itemLoad', () => {
        this.$refs.scroll.refresh()
      })*/
    },
    computed:{
      showGoods(){
        return this.goods[this.currentType].list
      }
    },
    mounted() {
      let newRefresh = debounce(this.$refs.scroll.refresh, 100)
      this.$bus.$on('homeImgLoad', () => {
        newRefresh()
      })
    },
    activated() {
      this.$refs.scroll.scrollTo(0, this.scrollY, 0)
      this.$refs.scroll.refresh()
    },
    deactivated() {
      this.scrollY = this.$refs.scroll.scroll.y
      this.$bus.$off('itemLoad', this.itemImgListener)
    },
    methods: {
      //请求banner数据
      getHomeMutidata(){
        getHomeMutidata().then(res => {
          this.banners = res.data.banner.list
          this.recommends = res.data.recommend.list
        })
      },
      //请求流行/新款/精选 数据
      getHomeGoods(type){
        const page = this.goods[type].page + 1
        getHomeGoods(type, page).then(res => {
          this.goods[type].list.push(...res.data.list)
          this.goods[type].page++
          this.$refs.scroll.finishPullUp()
        })
      },
      itemClick(index){
        switch (index){
          case 0:
            this.currentType = 'pop'
            break;
          case 1:
            this.currentType = 'new'
            break;
          case 2:
            this.currentType = 'sell'
        }
        this.$refs.tabControl1.currentIndex = index
        this.$refs.tabControl2.currentIndex = index
      },
      scrollBack(pos){
        this.isShowBack = (-pos.y) > 1000
        this.isTabFixed = (-pos.y) > this.offsetTopParent
      },
      upLoadMore(){
        this.getHomeGoods(this.currentType)
      },
      swiperLoad(){
        this.offsetTopParent = this.$refs.tabControl2.$el.offsetTop
      }
    }
  }
</script>

<style scoped>
#home{
  position: relative;
  height: 100vh;
}
.home-nav{
  background: var(--color-tint);
  color: #fff;
  position: relative;
  z-index: 9;
}
.scroll{
  overflow: hidden;
  height: calc(100% - 94px);
  margin-bottom: 50px;
}
.tabControl{
  position: fixed;
  left: 0;
  right: 0;
  top: 44px;
  z-index: 10;
}
</style>